package cn.wenhe9.yygh.cmn.controller;

import cn.wenhe9.yygh.cmn.service.DictService;
import cn.wenhe9.yygh.common.result.ResultResponse;
import cn.wenhe9.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author DuJinliang
 * 2022/2/15
 */
@Api(tags = "数据字典管理")
@RestController
//@CrossOrigin
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Resource
    private DictService dictService;

    /**
     * 根据数据id查询子数据列表
     * @param id 数据id
     * @return ResultResponse
     */
    @ApiOperation("根据数据id查询子数据列表")
    @GetMapping("/{id}")
    public ResultResponse<List<Dict>> findChildData(@PathVariable(name = "id") Long id){
        return ResultResponse.ok(dictService.findChildData(id));
    }

    /**
     * 导入数据字典
     * @param file MultipartFile
     * @return ResultResponse
     */
    @ApiOperation("导入数据字典")
    @PostMapping("/import")
    public ResultResponse importDict(MultipartFile file){
        return dictService.importDict(file);
    }

    /**
     * 导出数据字典接口
     * @param response HttpServletResponse
     */
    @ApiOperation("导出数据字典接口")
    @GetMapping("/export")
    public void exportDict(HttpServletResponse response){
        dictService.exportDictData(response);
    }

    /**
     * 根据dictCode和value查询
     * @param dictCode String
     * @param value String
     * @return ResultResponse
     */
    @GetMapping("/name/{dictCode}/{value}")
    public String getName(
            @PathVariable(name = "dictCode") String dictCode,
            @PathVariable(name = "value") String value
    ){
        return dictService.getDictName(dictCode, value);
    }

    /**
     * 根据value查询
     * @param value String
     * @return ResultResponse
     */
    @GetMapping("/name/{value}")
    public String getName(
            @PathVariable(name = "value") String value
    ){
        return dictService.getDictName(null, value);
    }

    /**
     * 根据dictCode获取下级节点
     * @param dictCode String
     * @return ResultResponse
     */
    @ApiOperation("根据dictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public ResultResponse findByDictCode(@PathVariable(name = "dictCode") String dictCode){
        return dictService.findByDictCode(dictCode);
    }
}
